package com.bdqn.gulimall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdqn.common.to.MemberPrice;
import com.bdqn.common.to.SkuReductionTo;
import com.bdqn.common.utils.PageUtils;
import com.bdqn.common.utils.Query;
import com.bdqn.gulimall.coupon.dao.SkuFullReductionDao;
import com.bdqn.gulimall.coupon.entity.MemberPriceEntity;
import com.bdqn.gulimall.coupon.entity.SkuFullReductionEntity;
import com.bdqn.gulimall.coupon.entity.SkuLadderEntity;
import com.bdqn.gulimall.coupon.service.MemberPriceService;
import com.bdqn.gulimall.coupon.service.SkuFullReductionService;
import com.bdqn.gulimall.coupon.service.SkuLadderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Resource
    SkuLadderService skuLadderService;//几件打几折

    @Resource
    MemberPriceService memberPriceService;//会员价格表，哪个会员等级对应的会员价格是什么



    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<SkuFullReductionEntity> queryWrapper = new QueryWrapper<>();
        String promotionSessionId = (String) params.get("promotionSessionId");// 场次id
        if (!StringUtils.isEmpty(promotionSessionId)){
            queryWrapper.eq("promotion_session_id",promotionSessionId);
        }
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReduction(SkuReductionTo reductionTo) {
        /**
         * 1、6.4、sku的优惠、满减等信息 gulimall_sms（另一个数据库）->sms_sku_ladder(几件打几折）\
         * sms_sku_full_reduction(满减表，满什么价减多少钱）\
         * sms_member_price(会员价格表，哪个会员等级对应的会员价格是什么）\
         * 需要调用远程服务gulimall-coupon
         */
        //sms_sku_ladder(几件打几折）
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();//几件打几折
        skuLadderEntity.setSkuId(reductionTo.getSkuId());
        skuLadderEntity.setFullCount(reductionTo.getFullCount());//满几件
        skuLadderEntity.setDiscount(reductionTo.getDiscount());//打几折
        skuLadderEntity.setAddOther(reductionTo.getCountStatus());//是否叠加其他优惠
        if (reductionTo.getFullCount() > 0){
            // skuLadderEntity.setPrice();//折后价
            skuLadderService.save(skuLadderEntity);
        }


        //2、sms_sku_full_reduction(满减表，满什么价减多少钱）
        SkuFullReductionEntity reductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(reductionTo,reductionEntity);
        //compareTo:和一个值进行比较,-1：比这个值小 0：和这个值一样 1：比这个值大
        if (reductionEntity.getFullPrice().compareTo(new BigDecimal("0"))==1){
            this.save(reductionEntity);
        }


        //3、sms_member_price(会员价格表，哪个会员等级对应的会员价格是什么）
        List<MemberPrice> memberPrice = reductionTo.getMemberPrice();
        List<MemberPriceEntity> collect = memberPrice.stream().map(item -> {
            MemberPriceEntity priceEntity = new MemberPriceEntity();
            priceEntity.setSkuId(reductionTo.getSkuId());
            priceEntity.setMemberLevelId(item.getId());//会员等级id
            priceEntity.setMemberLevelName(item.getName());//会员昵称
            priceEntity.setMemberPrice(item.getPrice());//当前会员等级对应的价格
            priceEntity.setAddOther(1);//是否叠加其他优惠,1代表默认叠加
            return priceEntity;
        }).filter(item->{
            //compareTo:和一个值进行比较,-1：比这个值小 0：和这个值一样 1：比这个值大
            return item.getMemberPrice().compareTo(new BigDecimal("0"))==1;
        }).collect(Collectors.toList());

        memberPriceService.saveBatch(collect);//批量保存

    }

}